from src.db.CcxDataCenterDb import CcxDataCenter, CcxDataCenterProd

report_ccxDataCenterProd = CcxDataCenterProd(logSql=False, autocommit=True)
report_ccxDataCenterLocal = CcxDataCenter(logSql=False, autocommit=True)


class Report:

    # 根据信用代码+年份抽取财报报表
    # validCheck=false 忽略
    # 财报有效性判断
    @staticmethod
    def QueryFinReportNew(creditCode: str, year: int, validCheck: bool):
        sql = """
                        SELECT  b.* from fin_report a
                    inner join `data-center`.fin_report_data b on a.sync_id =b.report_id 
                    where a.credit_code =%s and report_date =%s and release_status =1
                    and a.caliber =1 
                """
        if validCheck == True:
            """
                and a.valid =1
            """

        reportYear = str(year) + "-12-31"
        return report_ccxDataCenterProd.Query(sql, (creditCode, reportYear))

    # 根据信用代码+年份抽取财报报表
    @staticmethod
    def QueryFinReport(creditCode, year):
        sql = """
                 SELECT
                b.*,c.local_name 
                    from
                        fin_report a
                    inner join `data-center`.fin_report_data b on
                        a.sync_id = b.report_id
                    left join fin_report_subjects c on c.local_code =b.local_code and a.report_type =c.report_type  
                    where
                        a.credit_code = %s
                        and report_date = %s
                        and release_status = 1
                        and a.caliber = 1
                        and c.caliber =1
                        and a.valid = 1
                        and b.local_code in ('zx_monetary_cap','zx_acct_rcv','zx_red_monetary_cap_for_sale','zx_tot_oper_cost')
        """
        reportYear = str(year) + "-12-31"
        return Report.ReportToMap(report_ccxDataCenterProd.Query(sql, (creditCode, reportYear)))

    # 财报报表格式转换
    # List 转Map
    @staticmethod
    def ReportToMap(ReportData):
        ret = {}
        for report in ReportData:
            ret[report['local_name']] = report['value']
        return ret
